﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.Transitions;
using svs.Mobile.BeyondMedia.UiCore.GDI;

namespace svs.Mobile.BeyondMedia.Controls
{
	public class TransitionControl : Control
	{
		private readonly Win32.WindowProcCallback _delegate;
		private readonly IntPtr _wndprocReal; // the original wndproc of


		internal IViewTransition CurrentTransition  { get; set;}

		private Bitmap _offScreenBmp;
		private Graphics _offScreenGraphics;

		Rectangle _ContentRectangle = Rectangle.Empty;

		public TransitionControl()
		{
			_delegate = WnProc;
			_wndprocReal = Win32.SetWindowLong(Handle, Win32.GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(_delegate));
		}

		private int WnProc(IntPtr hwnd, uint msg, uint wParam, IntPtr lParam)
		{
			bool handled = false;
			if (msg == Win32.WM_PAINT)
			{
			  handled = OnPaint(hwnd);
			}

			return !handled ? Win32.CallWindowProc(_wndprocReal, hwnd, msg, wParam, lParam) : 0;
		}

		bool OnPaint(IntPtr hWnd)
		{
			if (CurrentTransition == null)
				return false;

			var ps = new Win32.PAINTSTRUCT();

			IntPtr hdc = Win32.BeginPaint(hWnd, ref ps);

			IntPtr hdcMem = _offScreenGraphics.GetHdc();

			using (Gdi g = Gdi.FromHdc(hdc, ps.rcPaint))
			{
				using (Gdi gMem = Gdi.FromHdc(hdcMem, Rectangle.Empty))
				{
					Rectangle rect = ps.rcPaint;

					try
					{
						CurrentTransition.DrawScreenOn(gMem, ps.rcPaint);
					}
					catch (Exception)
					{
						throw;
					}

					g.BitBlt(rect.Left, rect.Top, rect.Width, rect.Height, hdcMem, rect.Left, rect.Top,
					         Win32.ERop.SRCCOPY);
				}
			}

			_offScreenGraphics.ReleaseHdc(hdcMem);
			Win32.EndPaint(hWnd, ref ps);
			return true;
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{}

		public bool AllocateOffscreenBitmap()
		{
			if (_offScreenGraphics != null)
			{
				ReleaseOffscreenBitmap();
			}

			_ContentRectangle = ClientRectangle;

			try
			{
				_offScreenBmp = new Bitmap(ClientSize.Width, ClientSize.Height);
				_offScreenGraphics = Graphics.FromImage(_offScreenBmp);
			}
			catch (OutOfMemoryException)
			{
				_offScreenGraphics = null;
				return false;
			}

			return _offScreenGraphics != null;
		}

		public void ReleaseOffscreenBitmap()
		{
					if (_offScreenGraphics != null)
					{
						_offScreenGraphics.Dispose();
						_offScreenBmp.Dispose();
						_offScreenGraphics = null;
					}
		}


		public void RefreshControl()
		{
			_ContentRectangle = ClientRectangle;
			Win32.InvalidateRect(Handle, ref _ContentRectangle, false);
			Win32.UpdateWindow(Handle);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				ReleaseOffscreenBitmap();
			}

			base.Dispose(disposing);
		}
	}
}
